package com.tank.demo01;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

public class TankFrame extends Frame {

	private static final long serialVersionUID = 1L;

	Tank myTank = new Tank(200, 400, Dir.DOWN,Group.GOOD, this);

	List<Bullet> bullets = new ArrayList<Bullet>();
	List<Tank> tanks = new ArrayList<Tank>();
	
	// 爆炸
	List<Explode> explodes = new ArrayList<Explode>();

	static final int GAME_WIDTH = 800, GAME_HEIGHT = 600;

	public TankFrame() {
		setSize(GAME_WIDTH, GAME_HEIGHT);
		setResizable(false);
		setTitle("tank");
		setVisible(true);

		this.addKeyListener(new MyKeyListener());

		// 关闭窗口
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

	}

	Image offScreenImage = null;

	// 解决双缓冲
	@Override
	public void update(Graphics g) {
		if (offScreenImage == null) {
			// 和整体大小一致
			offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
		}
		// 图片画笔
		Graphics gOffScreen = offScreenImage.getGraphics();
		Color c = gOffScreen.getColor();
		gOffScreen.setColor(Color.white);
		gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
		gOffScreen.setColor(c);
		paint(gOffScreen);
		// 画到屏幕
		g.drawImage(offScreenImage, 0, 0, null);
	}

	@Override
	public void paint(Graphics g) {
		Color c = g.getColor();
		g.setColor(Color.BLACK);
		g.drawString("子弹数量：" + bullets.size(), 10, 60);
		g.drawString("敌人数量：" + tanks.size(), 10, 80);
		g.drawString("爆炸数量：" + explodes.size(), 10, 100);
		g.setColor(c);

		myTank.paint(g);

		// 要用这种方式才能用不需要在内部才能删除
		for (int i = 0; i < bullets.size(); i++) {
			bullets.get(i).paint(g);
		}

		// 画敌方坦克
		for (int i = 0; i < tanks.size(); i++) {
			tanks.get(i).paint(g);
		}

		for (int i = 0; i < bullets.size(); i++) {
			for (int j = 0; j < tanks.size(); j++) {
				bullets.get(i).collideWith(tanks.get(j));
			}
		}
		
		for (int i = 0; i < explodes.size(); i++) {
			explodes.get(i).paint(g);
		}
 
		/*
		 * for (Bullet b : bullets) { b.paint(g); }
		 */

		/*
		 * for (Iterator<Bullet> it = bullets.iterator();it.hasNext();) { Bullet b =
		 * it.next(); if (!b.live) { it.remove(); } }
		 */
	}

	class MyKeyListener extends KeyAdapter {

		boolean bL = false;
		boolean bU = false;
		boolean bR = false;
		boolean bD = false;

		// 按下去
		@Override
		public void keyPressed(KeyEvent e) {
			int key = e.getKeyCode();
			switch (key) {
			case KeyEvent.VK_LEFT:
				bL = true;
				break;
			case KeyEvent.VK_UP:
				bU = true;
				break;
			case KeyEvent.VK_RIGHT:
				bR = true;
				break;
			case KeyEvent.VK_DOWN:
				bD = true;
				break;
			default:
				break;
			}
			setMainTankDir();
		}

		// 抬起来
		@Override
		public void keyReleased(KeyEvent e) {
			int key = e.getKeyCode();
			switch (key) {
			case KeyEvent.VK_LEFT:
				bL = false;
				break;
			case KeyEvent.VK_UP:
				bU = false;
				break;
			case KeyEvent.VK_RIGHT:
				bR = false;
				break;
			case KeyEvent.VK_DOWN:
				bD = false;
				break;
			case KeyEvent.VK_CONTROL:
				myTank.fire();
				break;
			default:
				break;
			}
			setMainTankDir();
		}

		// 把处理交给坦克
		private void setMainTankDir() {

			if (!bL && !bU && !bR && !bD) {
				myTank.setMoving(false);
			} else {
				myTank.setMoving(true);
			}

			if (bL)
				myTank.setDir(Dir.LEFT);
			if (bU)
				myTank.setDir(Dir.UP);
			if (bR)
				myTank.setDir(Dir.RIGHT);
			if (bD)
				myTank.setDir(Dir.DOWN);
		}
	}

}
